<?php
/**
 * Created by PhpStorm.
 * User: HarrisHuang
 * Date: 17/5/8
 * Time: 22:33
 */
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\ImageList;
use App\Models\ImageGroup;
use Log;

class ImageController extends Controller
{
    public $baseUrl = 'http://ymd.dzfinvest.com/images';
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * 上传图片接口
     */
    public function upload(Request $request, ImageList $imageList)
    {
        if (isset($_FILES['file']) && $_FILES['file']['error'] == 0) {
            try {
                $type = ["jpg", "gif", "bmp", "jpeg", "png"];//设置允许上传文件的类型
                $path = base_path('public/images');
                if (!is_dir($path . '/' . date('Y'))) {
                    mkdir($path . '/' . date('Y'), 0755);
                }
                if (!is_dir($path . '/' . date('Y') . '/' . date('m'))) {
                    mkdir($path . '/' . date('Y') . '/' . date('m'), 0755);
                }
                $path = $path . '/' . date('Y') . '/' . date('m');
                $file = $_FILES['file'];
                $ext = substr(strrchr($file['name'], '.'), 1);
                //简单过滤文件后缀
                if (!in_array(strtolower($ext), $type)) {
                    return $this->error();
                }
                $filename = $_SERVER["REQUEST_TIME"] . rand(1000, 9999) . '.' . $ext;

                if (move_uploaded_file($file['tmp_name'], $path . '/' . $filename)) {
                    //保存到数据库
                    $saveUlr = date('Y') . '/' . date('m') . '/' . $filename;
                    $imageList->url = $saveUlr;
                    $imageList->status = 1;
                    $imageList->save();

                    return $this->success([
                        'url' => $saveUlr,
                        'showUlr' => $this->baseUrl . '/' . $saveUlr,
                    ]);
                }else{
                    return $this->error('上传失败');
                }
            }catch (\Exception $e){
                Log::error('error='.$e->getMessage());
                return $this->error();
            }
        }else{
            return $this->error();
        }
    }


    /**
     * 删除图片
     */
    public function delImage(Request $request, ImageList $imageList)
    {
        $id = (int)$request->get('id');
        $res = $imageList::destroy($id);
        if($res != 1){
            return $this->error();
        }
        return $this->success();
    }

    /**
     * 图片列表
     */
    public function lists(Request $request)
    {
        $gid = $request->get('gid', 0);
        $pageSize = $request->get('pageSize',10);
        if($gid > 0){
            $list = ImageList::where('gid',$gid)->paginate($pageSize);
        }else{
            $list = ImageList::paginate($pageSize);
        }
        $list = $list->toArray();
        if($list['total'] > 0){
            foreach ($list['data'] as $k => $item) {
                $list['data'][$k]['url'] = $this->baseUrl.'/'.$item['url'];
            }
        }
        $res = [
            'total' => $list['total'],
            'data' => $list['data'],
        ];
        return $this->success($res);
    }

    /**
     * 修改图片分组
     */
    public function updImgGroup(Request $request)
    {
        ImageList::where('id', (int)$request->input('id'))
            ->update(['gid' => (int)$request->input('gid')]);
        return $this->success();
    }

    public function addGroup(Request $request, ImageGroup $imageGroup)
    {
        $name = trim($request->input('name'));
        $imageGroup->name = $name;
        $imageGroup->status =1;
        $imageGroup->save();
        return $this->success();
    }

    public function deleteGroup(Request $request, ImageGroup $imageGroup)
    {
        $id = (int)$request->get('id');
        if($id <= 0){
            return $this->error();
        }
        $imageGroup::destroy($id);
        return $this->success();
    }

    public function groupList(Request $request, ImageGroup $imageGroup)
    {
        $list = $imageGroup::where('status',1)->orderBy('id','desc')->get();
        return $this->success($list);
    }



}
